I'm looking at the problem of drawing labels on a graph, e.g street names, which might overlap.
What I want, so I don't have to write it myself, is a module which takes 2 rectangles defined by their corners, and gives me a Boolean result as to whether or not the 2 rectangles overlap.
I don't want to be tied to any particular image processing software, e.g. Image::Magick, and I don't need the result to be the polygon defined by the overlap.
I can see an algorithm: Take the 2 end lines [*] of one rectangle, determine their equations, and see if they intersect any of the sides of the other rectangle. Then reverse the roles of the 2 rectangles to check the opposite case.
[*] For 2 lines meeting at the corner, it doesn't matter which of the 2 lines is used. So 2 bounding lines of a rectangle which don't meet ought to be sufficient.
Has this been done?
Are there any modules which do part of it?
I searched CPAN for Math::*, but did not see anything obviously relevant.
Re:Pretty straightforward calculation
Ron Savage on 2008-06-09T23:21:56
$many x $thanx;
Your comments gave me much to search for and read about.
Looks like the AABB is painless and the OBB is painful.
A = (x1,y1,x2,y2) and B = (u1,v1,u2,v2)
.Then A does not overlap B if one of these four tests pass:(x1,y1) (u1,v1)
+-------------+ +-----------+
| | | |
| A | | B |
| | | |
+-------------+ +-----------+
(x2,y2) (u2,v2)
So A overlaps B if none of the above hold:# A left of B
x2 < u1
# A right of B
x1 > u2
# A above B
y2 > v1
# A below B
y1 < v2
or equivalently (not ( x2 < u1 or x1 > u2 or y2 > v1 or y1 < v2 )
!(a or b) <=> !a and !b
)That's too simple to be a modulex2 >= u1 and x1 <= u2 and y2 <= v1 and y1 >= v2